package com.topvision.camerasdk.album.sensor;

import android.content.Context;
import android.graphics.Point;
import android.view.View;
import android.view.ViewGroup;

import com.topvision.camerasdk.R;
import com.topvision.commomsdk.uitl.AndroidUtils;


public class ParallelViewHelper implements GyroScopeSensorListener.ISensorListener {

    /**
     * 默认0.02f在宽度填满屏幕的图片上，移动起来看着很舒服
     */
    public static final float TRANSFORM_FACTOR = 0.1f;
    private float mTransformFactor = TRANSFORM_FACTOR;
    private View mParallelView;
    private float mCurrentShiftX;
    private float mCurrentShiftY;
    private GyroScopeSensorListener mSensorListener;
    private ViewGroup.LayoutParams mLayoutParams;
    private int mViewWidth;
    private int mViewHeight;
    private int mShiftDistancePX;
    Context context;

    public ParallelViewHelper(Context context) {
        this(context, context.getResources().getDimensionPixelSize(R.dimen.image_shift));
    }

    /**
     * 初始化一个
     *
     * @param context
     * @param shiftDistancePX
     */
    public ParallelViewHelper(Context context, int shiftDistancePX) {
        this.context = context;
        mShiftDistancePX = shiftDistancePX;
        mSensorListener = new GyroScopeSensorListener(context);
        mSensorListener.setSensorListener(this);

    }
    //TODO 原来的init方法
//    /**
//     * @param radio 放大比例
//     * create at 2017/2/7 13:12
//     */
//    private int mDistancePX;
//    public void init(final View targetView, int radio) {
//        if (radio == 1) {
//            mShiftDistancePX = context.getResources().getDimensionPixelSize(R.dimen.image_shift);
//        } else if (radio == 2) {
//            //放大两倍
//            mShiftDistancePX = mShiftDistancePX * 2;
//        }
//        mDistancePX = mShiftDistancePX;
//        mParallelView = targetView;
//        mParallelView.setX(-mShiftDistancePX);
//        mParallelView.setY(-mShiftDistancePX);
//        mLayoutParams = mParallelView.getLayoutParams();
//        //宽高只初始化一次
//        if(mViewWidth==0 && mViewHeight== 0){
//            mViewWidth = mParallelView.getWidth();
//            mViewHeight = mParallelView.getHeight();
//        }
//        if (mViewWidth > 0 && mViewHeight > 0) {
//            bindView();
//            return;
//        }
//
//    }

    /**
     * @param radio 放大比例
     * create at 2017/2/7 13:12
     */
    private int mDistancePX;
    public void init(final View targetView, int radio) {
        mShiftDistancePX = context.getResources().getDimensionPixelSize(R.dimen.image_shift);
        if (radio > 1) {
            //放大N倍
            mShiftDistancePX = mShiftDistancePX * radio;
        }
        mDistancePX = mShiftDistancePX;
        mParallelView = targetView;
        mParallelView.setX(-mShiftDistancePX);
        mParallelView.setY(-mShiftDistancePX);
        mLayoutParams = mParallelView.getLayoutParams();
        //宽高只初始化一次
        if(mViewWidth==0 && mViewHeight== 0){
            mViewWidth = mParallelView.getWidth();
            mViewHeight = mParallelView.getHeight();
        }
        if (mViewWidth > 0 && mViewHeight > 0) {
            bindView();
            return;
        }

    }


    void bindView() {
        mLayoutParams.width = mViewWidth + mShiftDistancePX * 2;
        mLayoutParams.height = mViewHeight + mShiftDistancePX * 2;
        mParallelView.setLayoutParams(mLayoutParams);
        if (listener != null) {
            listener.onPhotoViewZoomUp(mDistancePX,mViewWidth,mViewHeight);
        }
    }

    public void reset() {
        if(mParallelView!=null){
            Point point = AndroidUtils.getScreenPoint(context);
            mParallelView.setX(0);
            mParallelView.setY(0);
            mLayoutParams.width = point.x;
            mLayoutParams.height = point.y;
            mParallelView.setLayoutParams(mLayoutParams);
            if (listener != null) {
                listener.onPhotoViewZoomUp(mDistancePX,mViewWidth,mViewHeight);
            }
        }
    }


    /**
     * 注册监听陀螺仪事件
     */
    public void start() {
        mSensorListener.start();
    }

    /**
     * 监听陀螺仪事件耗电，因此在onPause里需要注销监听事件
     */
    public void stop() {
        mSensorListener.stop();
        reset();
    }

    /**
     * 设置移动的补偿变量，越高移动越快，标准参考{@link #TRANSFORM_FACTOR}
     *
     * @param transformFactor
     */
    public void setTransformFactor(float transformFactor) {
        mTransformFactor = transformFactor;
    }

    @Override
    public void onGyroScopeChange(float horizontalShift, float verticalShift) {
        mCurrentShiftX += mShiftDistancePX * horizontalShift * mTransformFactor;
        mCurrentShiftY += mShiftDistancePX * verticalShift * mTransformFactor;

        if (Math.abs(mCurrentShiftX) > mShiftDistancePX)
            mCurrentShiftX = mCurrentShiftX < 0 ? -mShiftDistancePX : mShiftDistancePX;

        if (Math.abs(mCurrentShiftY) > mShiftDistancePX)
            mCurrentShiftY = mCurrentShiftY < 0 ? -mShiftDistancePX : mShiftDistancePX;

        if(mParallelView!=null){
            //默认就margin 负的边距尺寸，因此 margin的最大值是 负的边距尺寸*2 ~ 0
            mParallelView.setX((int) mCurrentShiftX - mShiftDistancePX);
            mParallelView.setY((int) mCurrentShiftY - mShiftDistancePX);
        }
        //回调函数
        if (listener != null) {
            listener.onPhotoViewScale(mCurrentShiftX,mCurrentShiftY);
        }
    }

    /**
     * 设置一个回调接口
     */
    public PhotoViewScaleListener listener = null; //事件回调接口
    //回调接口
    public interface PhotoViewScaleListener {
        //移动
        void onPhotoViewScale(float currentShiftX, float currentShiftY);
        //放大
        void onPhotoViewZoomUp(int mDistancePX, int viewWidth, int viewHeight);
    }

    //设置回调接口
    public void setOnBigPicMoveListener(PhotoViewScaleListener photoViewScaleListener) {
        listener = photoViewScaleListener;
    }
}
